'use strict'
var UserModel = require('../../model/user');
var OrganizationModel = require('../../model/organization')
var Data = require('../../util/data')

class Register {
    async post(req, res, next) {
        try {
            const item = req.body
            if (!item.name || !item.phone || !item.password || !item.organizationId) {
                throw new Error('请补全信息！')
            } else {
                const users = await UserModel.insertMany(item)
                const user = users[0] || {}
                const organization = await OrganizationModel.findById(user.organizationId)
                organization.members.push(user._id)
                await OrganizationModel.findByIdAndUpdate(user.organizationId, organization)
                res.send(user)
            }
        } catch (err) {
            res.send(Data.throwError(err.message))
        }
    }
}

module.exports = new Register()